package com.siruier.boss.ui.activity.mall

import android.view.View
import androidx.core.view.isVisible
import com.kingja.loadsir.core.LoadService
import com.siruier.boss.api.core.launchUI
import com.siruier.boss.api.core.toData
import com.siruier.boss.api.mallApi
import com.siruier.boss.bean.ShopAddressBean
import com.siruier.boss.databinding.ActivitySelfUseSendBinding
import com.siruier.boss.ui.adapter.ConfirmOrderShopAdapter
import com.siruier.boss.ui.api.ApiObserver
import com.siruier.boss.ui.base.BaseActivity
import com.siruier.boss.ui.ext.gone
import com.siruier.boss.ui.ext.showLoadingCallback
import com.siruier.boss.ui.ext.toastMessageLong
import com.siruier.boss.ui.ext.visible
import com.siruier.boss.ui.helper.InjectBundle
import com.siruier.boss.ui.helper.loadsir.LoadSirHelper
import com.siruier.boss.ui.utils.FullyLinearLayoutManager
import com.siruier.boss.ui.utils.Logger

class SelfUseSendActivity : BaseActivity<ActivitySelfUseSendBinding>() {

    private val mLoadService: LoadService<*> by lazy { LoadSirHelper.register(vb.rootView) { onReload() } }
    private val mConfirmOrderShopAdapter: ConfirmOrderShopAdapter = ConfirmOrderShopAdapter()
    private val mOrderSn: String by InjectBundle()
    private var mShopAddressBean: ShopAddressBean? = null

    override fun initView() {
        bindViewClick(vb.viewAddessClick, vb.buttonSubmitOrder)
        vb.rvOrder.adapter = mConfirmOrderShopAdapter
        vb.rvOrder.layoutManager = FullyLinearLayoutManager(this)
    }

    override fun initData() {
        mLoadService.showLoadingCallback()
        onReload()
    }

    private fun onReload() {
        launchUI({
            changeAddress(mallApi.addressGetDefault().toData())
            mallApi.orderDetail(mOrderSn).toData()?.apply {
                mConfirmOrderShopAdapter.datas = mutableListOf(this)
                mConfirmOrderShopAdapter.notifyDataSetChanged()
            }
        }, ApiObserver(loadService = mLoadService))
    }

    override fun onClick(v: View?) {
        when (v) {
            vb.viewAddessClick -> launchStartActivityForResult(
                ReceivingAddressActivity::class.java,
                options = { putBoolean(ReceivingAddressActivity.CLICK_FINISH, true) }) {
                changeAddress(it.data?.getParcelableExtra(ReceivingAddressActivity.RECEIVING_ADDRESS))
            }
            vb.buttonSubmitOrder -> {
                if (mShopAddressBean == null) {
                    "请选择地址".toastMessageLong()
                    return
                }
                launchUI({ mallApi.sendShip(mShopAddressBean?.id, mOrderSn).toData() }, ApiObserver(this) {
                    "发货成功".toastMessageLong()
                    setResult(RESULT_OK)
                    finish()
                })
            }
        }
    }

    private fun changeAddress(shopAddressBean: ShopAddressBean?) {
        mShopAddressBean = shopAddressBean
        mShopAddressBean?.run {
            vb.viewAddressEmpty.gone()
            vb.groupAddress.visible()
            vb.tvName.text = name
            vb.tvPhome.text = phone
            vb.tvAddressDetails.text = province + city + county + address
        }
    }
}